//
//  SlideBarCell.m
//  SlideTabBar
//
//  Created by Mr.LuDashi on 15/6/30.
//  Copyright (c) 2015年 李泽鲁. All rights reserved.
//

#import "SlideBarCell.h"
#import "WJAdvertCircle.h"

#define kImageCount 4
@interface SlideBarCell ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation SlideBarCell

//- (instancetype)initWithFrame:(CGRect)frame {
//
//    self = [super initWithFrame:frame];
//    
//    if (self) {
//    
//        NSLog(@"---------%f  ----------- %f",self.frame.size.width,self.frame.size.height);
//    }
//    return self;
//}
//
//- (void)layoutSubviews {
//
//    [super layoutSubviews];
////    [self showLaunchAdvert];
//    NSLog(@"========%f  ========== %f %@",self.frame.size.width,self.frame.size.height,self.bounds);
//}



- (void)layoutSubviews {

    [super layoutSubviews];
    NSLog(@"0000000%f",self.bounds.size.height);
    self.scrollView.bounces = NO;
//    self.scrollView.frame = self.bounds;
    CGFloat scrollViewW = self.bounds.size.width;
    CGFloat scrollViewH = self.bounds.size.height;
    for ( int i = 1; i <= kImageCount; i++) {
        CGFloat imageViewX = (i - 1)*scrollViewW;
        CGFloat imageViewY = 0;
        CGFloat imageViewW = scrollViewW;
        CGFloat imageViewH = scrollViewH;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH)];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"1439%d",i]];
        [self.scrollView addSubview:imageView];
        
    }
    self.scrollView.contentSize = CGSizeMake(scrollViewW * kImageCount, 0);
    // 设置为可分页的形式
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
    
    // 设置轮播下面的点
    UIPageControl *pageController = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    CGPoint tempPoint = self.center;
    tempPoint.y += self.frame.origin.y * 0.8;
    pageController.center = tempPoint;
    pageController.pageIndicatorTintColor = [UIColor whiteColor];
    pageController.currentPageIndicatorTintColor = [UIColor purpleColor];
    pageController.numberOfPages = kImageCount;
    
    // 把上面定义的pageController 赋给pageControl  因为pageController是局部变量括号结束就消失
    self.pageControl = pageController;
    [self addSubview:self.pageControl];
    
    [self addtimer];
   
}

- (void)addtimer {

    // 设置pageControl跳动的时间和周期
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(cycleImage) userInfo:nil repeats:YES];
}

- (void)removTimer {

    [self.timer invalidate];
    self.timer = nil;
}

- (void)cycleImage {
    
    // 设置点轮播
//    CGFloat moveX = 0;
    if (self.pageControl.currentPage == 4) {
        
        self.pageControl.currentPage = 0;

    }else {
        
        self.pageControl.currentPage ++;
        
    }
   CGFloat moveX = self.pageControl.currentPage * self.scrollView.frame.size.width;
    CGPoint tempPoint = CGPointMake(moveX, 0);
//    self.scrollView.contentOffset = tempPoint;
    [self.scrollView setContentOffset:tempPoint animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 设置内容抵消
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    CGFloat scrollViewW = scrollView.frame.size.width;
    // 设置你发动到一半就跳覆盖全屏幕
    self.pageControl.currentPage = (contentOffSetX + scrollViewW * 0.5) / scrollViewW;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [self removTimer];
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//
//    [self addtimer];
//}


@end
